package jdklearn.nio.复制文件;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.time.LocalDateTime;

/**
 * create at 2018/10/23
 * author: suveng
 * email: suveng@163.com
 **/
public class Main {
    public static void main(String[] args) throws IOException {
        LocalDateTime pretime=LocalDateTime.now();
//        创建buffer
        FileOutputStream fileOutputStream = new FileOutputStream("testnio_copy");
        FileChannel outChannel = fileOutputStream.getChannel();
        FileInputStream fileInputStream = new FileInputStream("testnio");
        FileChannel inChannel = fileInputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
//        读取源文件 写入目标文件

        while (true) {
            int eof=inChannel.read(byteBuffer);
            if (eof==-1){
                break;
            }
            byteBuffer.flip();
            outChannel.write(byteBuffer);
            byteBuffer.clear();
        }
        LocalDateTime aftertime=LocalDateTime.now();

        System.out.println(aftertime.toString());



    }
}
